package com.bee.portal.web.dwr;

import java.util.List;
import java.util.Map;

import com.bee.base.entity.Page;
import com.bee.base.web.dwr.BaseDwr;
import com.bee.portal.domain.sm.Account;
import com.bee.portal.domain.sm.Dictionary;
import com.bee.portal.domain.sm.DictionaryItem;
import com.bee.portal.domain.sm.Group;
import com.bee.portal.domain.sm.Organization;
import com.bee.portal.domain.sm.Parameter;
import com.bee.portal.domain.sm.Post;
import com.bee.portal.domain.sm.Resource;
import com.bee.portal.domain.sm.Role;
import com.bee.portal.domain.sm.User;
import com.bee.portal.service.sm.AccountService;
import com.bee.portal.service.sm.DictionaryItemService;
import com.bee.portal.service.sm.DictionaryService;
import com.bee.portal.service.sm.GroupService;
import com.bee.portal.service.sm.OrganizationService;
import com.bee.portal.service.sm.ParameterService;
import com.bee.portal.service.sm.PostService;
import com.bee.portal.service.sm.ResourceService;
import com.bee.portal.service.sm.RoleService;
import com.bee.portal.service.sm.UserService;

public class SmDwr extends BaseDwr {

	private static final long serialVersionUID = 7047456396482348759L;

	private AccountService accountService;
	private DictionaryService dictionaryService;
	private DictionaryItemService dictionaryItemService;
	private GroupService groupService;
	private OrganizationService organizationService;
	private ParameterService parameterService;
	private PostService postService;
	private ResourceService resourceService;
	private RoleService roleService;
	private UserService userService;

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public void setDictionaryItemService(DictionaryItemService dictionaryItemService) {
		this.dictionaryItemService = dictionaryItemService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public void setPostService(PostService postService) {
		this.postService = postService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getCurrentUser() {
		try {
			User user = new User();
			user.setSid(1L);
			user.setCname("系统管理员");
			return user;
		} catch (Exception e) {
			return new User();
		}
	}

	// Account
	public void saveAccount(Account entity) {
		entity.setSmOrganizationSid(10000L);
		accountService.save(entity);
	}

	public Account getAccount(Long sid) {
		return accountService.get(sid);
	}

	public void updateAccount(Account entity) {
		accountService.update(entity);
	}

	public void deleteAccount(Long sid) {
		Account entity = this.getAccount(sid);
		entity.setStatus(0);
		this.updateAccount(entity);
	}

	public Page pageAccount(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return accountService.page(params, pageNo, pageSize);
	}

	// Dictionary
	public void saveDictionary(Dictionary entity) {
		entity.setSmOrganizationSid(10000L);
		dictionaryService.save(entity);
	}

	public Dictionary getDictionary(Long sid) {
		return dictionaryService.get(sid);
	}

	public List<Dictionary> findDictionary(Map<String, String> params) {
		return dictionaryService.find(params);
	}

	public void updateDictionary(Dictionary entity) {
		dictionaryService.update(entity);
	}

	public void deleteDictionary(Long sid) {
		Dictionary entity = this.getDictionary(sid);
		entity.setStatus(0);
		this.updateDictionary(entity);
	}

	public Page pageDictionary(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return dictionaryService.page(params, pageNo, pageSize);
	}

	// DictionaryItem
	public void saveDictionaryItem(DictionaryItem entity) {
		entity.setSmOrganizationSid(10000L);
		dictionaryItemService.save(entity);
	}

	public DictionaryItem getDictionaryItem(Long sid) {
		return dictionaryItemService.get(sid);
	}

	public void updateDictionaryItem(DictionaryItem entity) {
		dictionaryItemService.update(entity);
	}

	public void deleteDictionaryItem(Long sid) {
		DictionaryItem entity = this.getDictionaryItem(sid);
		entity.setStatus(0);
		this.updateDictionaryItem(entity);
	}

	public Page pageDictionaryItem(Map<String, String> params, Map<String, String> likeMap,
			Map<String, String> sortMap, int pageNo, int pageSize) {
		return dictionaryItemService.page(params, pageNo, pageSize);
	}

	// Group
	public void saveGroup(Group entity) {
		entity.setSmOrganizationSid(10000L);
		groupService.save(entity);
	}

	public Group getGroup(Long sid) {
		return groupService.get(sid);
	}

	public void updateGroup(Group entity) {
		groupService.update(entity);
	}

	public void deleteGroup(Long sid) {
		Group entity = this.getGroup(sid);
		entity.setStatus(0);
		this.updateGroup(entity);
	}

	public Page pageGroup(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return groupService.page(params, pageNo, pageSize);
	}

	// Organization
	public void saveOrganization(Organization entity) {
		entity.setSmOrganizationSid(10000L);
		organizationService.save(entity);
	}

	public Organization getOrganization(Long sid) {
		return organizationService.get(sid);
	}

	public void updateOrganization(Organization entity) {
		organizationService.update(entity);
	}

	public void deleteOrganization(Long sid) {
		Organization entity = this.getOrganization(sid);
		entity.setStatus(0);
		this.updateOrganization(entity);
	}

	public Page pageOrganization(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return organizationService.page(params, pageNo, pageSize);
	}

	// Parameter
	public void saveParameter(Parameter entity) {
		entity.setSmOrganizationSid(10000L);
		parameterService.save(entity);
	}

	public Parameter getParameter(Long sid) {
		return parameterService.get(sid);
	}

	public void updateParameter(Parameter entity) {
		parameterService.update(entity);
	}

	public void deleteParameter(Long sid) {
		Parameter entity = this.getParameter(sid);
		entity.setStatus(0);
		this.updateParameter(entity);
	}

	public Page pageParameter(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return parameterService.page(params, pageNo, pageSize);
	}

	// Post
	public void savePost(Post entity) {
		entity.setSmOrganizationSid(10000L);
		postService.save(entity);
	}

	public Post getPost(Long sid) {
		return postService.get(sid);
	}

	public void updatePost(Post entity) {
		postService.update(entity);
	}

	public void deletePost(Long sid) {
		Post entity = this.getPost(sid);
		entity.setStatus(0);
		this.updatePost(entity);
	}

	public Page pagePost(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return postService.page(params, pageNo, pageSize);
	}

	// Resource
	public void saveResource(Resource entity) {
		entity.setSmOrganizationSid(10000L);
		resourceService.save(entity);
	}

	public Resource getResource(Long sid) {
		return resourceService.get(sid);
	}

	public void updateResource(Resource entity) {
		resourceService.update(entity);
	}

	public void deleteResource(Long sid) {
		Resource entity = this.getResource(sid);
		entity.setStatus(0);
		this.updateResource(entity);
	}

	public Page pageResource(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return resourceService.page(params, pageNo, pageSize);
	}

	// Role
	public void saveRole(Role entity) {
		entity.setSmOrganizationSid(10000L);
		roleService.save(entity);
	}

	public Role getRole(Long sid) {
		return roleService.get(sid);
	}

	public void updateRole(Role entity) {
		roleService.update(entity);
	}

	public void deleteRole(Long sid) {
		Role entity = this.getRole(sid);
		entity.setStatus(0);
		this.updateRole(entity);
	}

	public Page pageRole(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return roleService.page(params, pageNo, pageSize);
	}

	// User
	public void saveUser(User entity) {
		entity.setSmOrganizationSid(10000L);
		userService.save(entity);
	}

	public User getUser(Long sid) {
		return userService.get(sid);
	}

	public void updateUser(User entity) {
		userService.update(entity);
	}

	public void deleteUser(Long sid) {
		User entity = this.getUser(sid);
		entity.setStatus(0);
		this.updateUser(entity);
	}

	public Page pageUser(Map<String, String> params, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return userService.page(params, pageNo, pageSize);
	}

	/**
	 * 通过角色获取资源列表
	 * 
	 * @param sid
	 * @return
	 */
	public List<Map<String, Object>> findResourceByRole(Long sid) {
		return resourceService.findResourceByRole(sid);
	}

	/**
	 * 保存角色资源关联关系
	 * 
	 * @param sid
	 * @param ids
	 */
	public void saveRoleAndResource(Long sid, List<Long> ids) {
		roleService.saveRoleAndResource(sid, ids);
	}

	/**
	 * 通过父节点获取有效的资源列表
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findMenu(Long parentSid) {
		return resourceService.findMenu(parentSid);
	}

	/**
	 * 通过父节点获取有效的资源列表展开
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findMenuEx(Long parentSid) {
		return resourceService.findMenuEx(parentSid, false);
	}

	/**
	 * 通过父节点获取有效的资源列表展开
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findMenuExp(Long parentSid) {
		return resourceService.findMenuEx(parentSid, true);
	}

	/**
	 * 通过父节点获取当前用户有效的资源列表
	 * 
	 * 
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findMenuByUser(Long parentSid) {
		Long userSid = this.getCurrentUser().getSid();
		return resourceService.findMenuByUser(parentSid, userSid);
	}

	/**
	 * 通过父节点获取当前用户有效的资源列表展开
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findMenuByUserEx(Long parentSid) {
		Long userSid = this.getCurrentUser().getSid();
		return resourceService.findMenuByUserEx(parentSid, userSid, false);
	}

	/**
	 * 通过父节点获取当前用户有效的资源列表展开
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findMenuByUserExp(Long parentSid) {
		Long userSid = this.getCurrentUser().getSid();
		return resourceService.findMenuByUserEx(parentSid, userSid, true);
	}

	/**
	 * 通过父节点获取有效的组织结构列表
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findOrg(Long parentSid) {
		return organizationService.findOrg(parentSid);
	}

	/**
	 * 通过父节点获取有效的组织结构列表展开
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findOrgEx(Long parentSid) {
		return organizationService.findOrgEx(parentSid, false);
	}

	/**
	 * 通过父节点获取有效的组织结构列表展开
	 * 
	 * @param parentSid
	 * @return
	 */
	public List<Map<String, Object>> findOrgExp(Long parentSid) {
		return organizationService.findOrgEx(parentSid, true);
	}

}
